package com.easylinkin.linkappapi.security.controller;

import com.easylinkin.linkappapi.application.service.ApplicationService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import com.easylinkin.linkappapi.security.entity.LinkappPrivilege;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import com.easylinkin.linkappapi.security.entity.RoleRefPrivilegeDTO;
import com.easylinkin.linkappapi.security.service.LinkappPrivilegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import site.morn.boot.data.CrudControllerSupport;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;
import site.morn.rest.RestModel;
import site.morn.validate.group.Search;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * 权限管理
 *
 * @author wujie
 * @since 0.1.1,  2019/08/28
 */
@Api(tags = "权限管理")
@RestController
@RequestMapping("/linkappPrivilege")
public class LinkappPrivilegeController extends CrudControllerSupport<LinkappPrivilege, Long, LinkappPrivilegeService> {

    @Autowired
    ApplicationService applicationService;

    @Resource
    LinkappUserContextProducer linkappUserContextProducer;

    @ApiOperation("查询所有权限列表")
    @PostMapping({"searchAll"})
    public RestMessage searchAll(
            @Validated({Search.class}) @RequestBody RestModel<LinkappPrivilege> restModel) {
        List<LinkappPrivilege> privileges = service().selectPrivilegeAll(restModel.getModel());
        return RestBuilders.successMessage(privileges);
    }

    @ApiOperation("查询当前租户应用模板下配置的权限列表")
    @PostMapping({"/selectApplicationPrivilegeByUser"})
    public RestMessage selectApplicationPrivilegeByUser(
            @Validated({Search.class}) @RequestBody RestModel<LinkappPrivilege> restModel) {
        Long userId = linkappUserContextProducer.getCurrent().getId();
        //增加PC端默认值type
        List<LinkappPrivilege> privileges = applicationService.selectApplicationPrivilegeByUser(userId.toString(), restModel.getModel().getFlag());
        return RestBuilders.successMessage(privileges);
    }

    @ApiOperation(value = "权限树查询,全量查询")
    @GetMapping("searchTree/{disabled}")
    public RestMessage searchPrivilegeTree(@PathVariable Integer disabled) {
        return RestBuilders.successMessage(service().searchPrivilegeTree(disabled));
    }

    @ApiOperation("根据角色ID获取对应关联权限数据")
    @GetMapping("/selectPrivilegeByRole/{roleId}")
    public RestMessage selectSpaceByRole(@PathVariable String roleId, @RequestParam(value = "type", required = false) Integer type) {
        return RestBuilders.successBuilder().data(service().selectPrivilegeByRole(roleId, type)).build();
    }

    /**
     * 添加type   兼容小程序 权限
     *
     * @param type
     * @return
     */
    @ApiOperation("根据类型获取菜单权限树、或者小程序的权限树")
    @GetMapping("/selectPrivilegeByUser")
    public RestMessage selectPrivilegeByUser(@RequestParam(value = "type", required = false) Integer type) {
        LinkappUser user = linkappUserContextProducer.getCurrent();
        Set<LinkappPrivilege> list = new TreeSet<LinkappPrivilege>();
        if ("1".equals(user.getType())) {
            List<LinkappPrivilege> privileges = applicationService.selectApplicationPrivilegeByUser(user.getId().toString(), type);
            for (LinkappPrivilege privilege : privileges) {
                if (privilege.getType() == 1) {
                    list.add(privilege);
                }
            }
        } else {
            TreeSet<LinkappPrivilege> privileges = service().selectPrivilegeByUser(user.getId().toString(), type);
            for (LinkappPrivilege privilege : privileges) {
                if (privilege.getType() == 1) {
                    list.add(privilege);
                }
            }
        }
        return RestBuilders.successBuilder().data(list).build();
    }

    /**
     * 添加type   兼容小程序 权限
     *
     * @param type
     * @return
     */
    @ApiOperation("根据类型获取个性化菜单权限树、或者小程序的权限树")
    @GetMapping("/selectPrivilegeCustomByUser")
    public RestMessage selectPrivilegeCustomByUser(@RequestParam(value = "type", required = false) Integer type) {
        LinkappUser user = linkappUserContextProducer.getCurrent();
        Set<LinkappPrivilege> list = new TreeSet<LinkappPrivilege>();
        if ("1".equals(user.getType())) {
            List<LinkappPrivilege> privileges = applicationService.selectApplicationPrivilegeCustomByUser(user.getTenantId(), user.getId().toString(), type);
            for (LinkappPrivilege privilege : privileges) {
                if (privilege.getType() == 1 || privilege.getType() == 0) {
                    list.add(privilege);
                }
            }
        } else {
            TreeSet<LinkappPrivilege> privileges = service().selectPrivilegeCustomByUser(user.getTenantId(), user.getId().toString(), type);
            for (LinkappPrivilege privilege : privileges) {
                if (privilege.getType() == 1 || privilege.getType() == 0) {
                    list.add(privilege);
                }
            }
        }
        return RestBuilders.successBuilder().data(list).build();
    }

    @ApiOperation("修改、新增角色-菜单权限关联")
    @PostMapping("/privilege2Role")
    public RestMessage privilege2Role(@RequestBody RoleRefPrivilegeDTO roleRefPrivilegeDTO) {
        service().privilege2Role(roleRefPrivilegeDTO.getRole(), roleRefPrivilegeDTO.getPrivileges(), roleRefPrivilegeDTO.getType());
        return RestBuilders.successMessage();
    }

    @ApiOperation("新增菜单权限")
    @PostMapping("/addPrivilege")
    public RestMessage addPrivilege(@RequestBody LinkappPrivilege linkappPrivilege) {
        Assert.notNull(linkappPrivilege, "linkappPrivilege 不能为空");
        Assert.notNull(linkappPrivilege.getName(), "name 不能为空");
        Assert.notNull(linkappPrivilege.getLevel(), "level 不能为空");
        Assert.notNull(linkappPrivilege.getSort(), "sort 不能为空");
        Assert.notNull(linkappPrivilege.getType(), "type 不能为空");
        service().addPrivilege(linkappPrivilege);
        return RestBuilders.successMessage();
    }

    @ApiOperation("根据菜单权限ID修改菜单权限")
    @PostMapping("/updatePrivilege")
    public RestMessage updatePrivilege(@RequestBody LinkappPrivilege linkappPrivilege) {
        Assert.notNull(linkappPrivilege, "linkappPrivilege 不能为空");
        Assert.notNull(linkappPrivilege.getId(), "id 不能为空");
        Assert.notNull(linkappPrivilege.getName(), "name 不能为空");
        Assert.notNull(linkappPrivilege.getLevel(), "level 不能为空");
        Assert.notNull(linkappPrivilege.getSort(), "sort 不能为空");
        Assert.notNull(linkappPrivilege.getType(), "type 不能为空");
        service().updatePrivilege(linkappPrivilege);
        return RestBuilders.successMessage();
    }


}
